package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.commons.cache.anno.MyCacheEvict;
import org.example.commons.cache.anno.MyCacheable;
import org.example.dto.TaskDataDTO;
import org.example.service.TaskDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CacheConfig(cacheNames = "taskData")
@Service
@Slf4j
public class TaskDataServiceImpl implements TaskDataService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public TaskDataDTO getTaskDataDTO(String id) {
        log.info("getTaskDataDTO... id={}", id);
        if (ObjectUtils.isEmpty(id)) return null;
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
        String sql = "select id as \"id\", " +
                "name as \"name\", " +
                "create_time as \"createTime\" " +
                "from pm_data_object where id=:id";
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
//        List<Map<String, Object>> maps = namedParameterJdbcTemplate.queryForList(sql, params);
//        System.out.println(maps);
        List<TaskDataDTO> list = namedParameterJdbcTemplate.query(sql, params, new BeanPropertyRowMapper<TaskDataDTO>(TaskDataDTO.class));
//        List<TaskDataDTO> list = namedParameterJdbcTemplate.queryForList(sql, params, TaskDataDTO.class);
        if (ObjectUtils.isEmpty(list)) return null;
        return list.get(0);
//        return null;
    }

    @MyCacheable(key = "'taskDataDTO:'+#id")
    @Cacheable(key = "'taskDataDTO:'+#id", cacheManager = "ehcacheManager")
    @Override
    public TaskDataDTO haveCacheTest(String id) {
        log.info("haveCacheTest... id={}", id);
        return this.getTaskDataDTO(id);
    }

    @Override
    @MyCacheEvict(key = "'taskDataDTO:'+#taskDataDTO.id", delytime = 500)
    @CacheEvict(key = "'taskDataDTO:'+#taskDataDTO.id")
    @Transactional
    public void saveTaskData(TaskDataDTO taskDataDTO) {
        log.info("saveTaskData... id={}", taskDataDTO.getId());
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
        String sql = "insert into pm_data_object(id,name,create_time) values(:id, :name, :createTime)";
        Map<String, Object> params = new HashMap<>();
        params.put("id", taskDataDTO.getId());
        params.put("name", taskDataDTO.getName());
        params.put("createTime", new Date());
        namedParameterJdbcTemplate.update(sql, params);
    }


}
